<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 18:08
 */
namespace app\admin\service;


class QrcodeService
{
    //https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getQRCode.html
    //https://blog.csdn.net/qq_39870660/article/details/129441645
    public $text = '';
    public function getQrcodeText($user_id){
        // https://h5.coderyun.cn/#/pages/qrcode/index?token=

        //yueke.coderyu.cn    csksd.cn
        if(request()->isCli()){
//            $host = "https://spacestudiohk.com/mobile/";
            $host = "https://yueke.coderyun.cn/mobile";
        }else{
            $host = "https://".$_SERVER['HTTP_HOST']."/mobile/";
        }

        $token = aes_encryption_encode($user_id);

        if(request()->isCli()){
            $text = $host."#/pages/qrcode/index?token={$token}";
        }else{
            $text = $host."#/pages/qrcode/index?token={$token}";
        }


        return $text;
    }
    public function makeQroce($text,$user_id){
//        \PHPQRCode\QRcode::png("Test", "/tmp/qrcode.png", 'L', 4, 2);
        $pathname = ROOT_PATH . 'public/storage/qrcode';
        if(!is_dir($pathname)) { //若目录不存在则创建之
            mkdir($pathname,0777,true);
        }


        //二维码图片保存路径(若不生成文件则设置为false)
        $filename = $pathname . "/" . time().uniqid() . ".png";
        if(file_exists($filename)){
            unlink($filename);
        }
        //二维码容错率，默认L
        $level = "L";
        //二维码图片每个黑点的像素，默认4
        $size = '4';
        //二维码边框的间距，默认2
        $padding = 2;
        //保存二维码图片并显示出来，$filename必须传递文件路径
        $saveandprint = true;

        //生成二维码图片
        \PHPQRCode\QRcode::png($text,$filename,$level,$size,$padding,$saveandprint);
        return $filename;

        //二维码logo
        $QR = imagecreatefromstring(file_get_contents($filename));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);
        $logo_width = imagesx($logo);
        $logo_height = imagesy($logo);
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        imagepng($QR,$filename);
        return $filename;
    }


    /**
     * @param $backgroundImagePath
     * @param $logoImagePath  png
     */
    public function mergeImage($backgroundImagePath,$logoImagePath,$newImagePath){
        if(file_exists($newImagePath)){
            unlink($newImagePath);
        }
        // 获取图片信息
        list($width, $height, $type) = getimagesize($backgroundImagePath);
        // 加载背景图和logo图
        switch ($type) {
            case IMAGETYPE_JPEG:
                $backgroundImage = imagecreatefromjpeg($backgroundImagePath);
                break;
            case IMAGETYPE_PNG:
                $backgroundImage = imagecreatefrompng($backgroundImagePath);
                break;
            case IMAGETYPE_GIF:
                $backgroundImage = imagecreatefromgif($backgroundImagePath);
                break;
            default:
                return false; // 不支持的图片类型
        }
        $logoImage = imagecreatefrompng($logoImagePath);

        // 获取logo图片的宽高
        $logoWidth = imagesx($logoImage);
        $logoHeight = imagesy($logoImage);

        // 获取背景图的宽高
        $backgroundWidth = imagesx($backgroundImage);
        $backgroundHeight = imagesy($backgroundImage);

        // 计算合成位置，这里我们将logo放在背景图的右下角
        $x = $backgroundWidth - $logoWidth - 30; // 10是右边距
        $y = $backgroundHeight - $logoHeight - 15; // 10是下边距

        // 将logo合成到背景图上
//        imagecopy($backgroundImage, $logoImage, $x, $y, 0, 0, $logoWidth, $logoHeight);
        imagecopyresampled($backgroundImage, $logoImage, $x, $y, 0, 0, $logoWidth, $logoHeight, $logoWidth, $logoHeight);
        imagepng($backgroundImage,$newImagePath);


        // 清理内存
        imagedestroy($backgroundImage);
        imagedestroy($logoImage);
        return $newImagePath;
    }
}
